<?php
echo '<html>';
echo '<body>';
foreach ($_POST as $key => $value) {
    echo 'Debug->$_POST: ' . $key . '->' . $value . '<br />';
}
foreach ($_GET as $key => $value) {
    echo 'Debug->$_GET: ' . $key . '->' . $value . '<br />';
}

$section = $_GET('section');
if(isset ($_GET('filter'))){
    $filter = $_GET('filter');
    $arrayFilter = plexGetFilter($section, $filter);
} else {
    $arrayFilter = plexGetFilter($section);
}

echo json_encode($arrayFilter);

echo '</body>';
echo '</html>';

function plexGetFilter($section, $category = NULL) {
    global $config, $logger;
    
    if ($category == NULL){
        $url = $config['plex_url'] . '/library/sections/' . $section;
        $logger->info('Getting filters from the Plex library at ' . $url);
    } else {
        $url = $config['plex_url'] . '/library/sections/' . $section . '/' . $category;
        $logger->info('Searching for categories in the Plex library at ' . $url);
    }
    
    $xml = load_xml_from_url($url);
    if (!$xml) {
        $logger->info('No XML found at url: ' . $url);
        return FALSE;
    }
    
    if($xml['viewGroup'] == 'secondary'){
        foreach ($xml->Directory as $option) {
            $options[$option['title']] = $option['key'];
        }
    } else {
        $logger->info('We hit a movie listing at url: ' . $url);
        return TRUE;
    }
    return $options;
}
?>
